package com.lagou.bean;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.lagou.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

@Component
public class ComsumerComponet {

    @Reference
    private HelloService  helloService;

    //创建固定线程池
//    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

    private static int coreSize = Runtime.getRuntime().availableProcessors();

    private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("hello-pool-%d").build();

    private static ExecutorService fixedThreadPool = new ThreadPoolExecutor(coreSize, coreSize * 2
            , 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10), THREAD_FACTORY, new ThreadPoolExecutor.DiscardOldestPolicy());

    private static int count=0;

    public String  sayHello(String name){

        return  helloService.sayHello(name);
    }

    public String doMethod(String name, long startTime){

        fixedThreadPool.submit(() -> {
            helloService.methodA(name);
            helloService.methodB(name);
            helloService.methodC(name);
        });
        count++;
        if (count == 2000) {
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            System.out.println("2000次时间为：" + time + "ms");
            return "======2000次时间为：" + time + "ms====";
        }else{
            if(count>2000){
                return null;
            }
            return "执行到第"+count+"次了";
        }
    }

}
